package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoSyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CartInfoSyncServiceImpl implements CartInfoSyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    //2(不重要).修改购物车DB中的数量
    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {
        System.out.println("子线程异步修改DB数据：" + Thread.currentThread().getName());
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
        .eq("sku_id",cartInfo.getSkuId())
        .eq("user_id",cartInfo.getUserId()));
    }

    //（不重要） 2:保存购物车到DB中
    @Override
    @Async
    public void insertCartInfo(CartInfo cartInfo) {
        System.out.println("子线程异步保存DB数据：" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }
}
